//
//  KYMineController.m
//  KYKJPackDemo
//
//  Created by Jordan zhou on 2017/10/10.
//  Copyright © 2017年 ???. All rights reserved.
//

#define KYTableBorderW 6

#import "KYMineController.h"
#import "KYUserInfoController.h"
#import "KYMineStatusController.h"
#import "SPKitExample.h"
@interface KYMineController ()<JSTAlertViewDelegate>

@end

@implementation KYMineController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    // 0.设置标题
    self.title = NSLocalizedString(@"个人中心", nil);
    
    // 1.添加分组
    [self setUpGroup0];
    
    // 2.设置footer
    [self setUpFooter];
}

- (void)setUpFooter
{
    // 按钮
    UIButton *logoutButton = [[UIButton alloc] init];
    CGFloat logoutX = KYTableBorderW + 2;
    CGFloat logoutY = 0;
    CGFloat logoutW = self.tableView.frame.size.width - 2 * logoutX;
    CGFloat logoutH = 45;
    logoutButton.frame = CGRectMake(logoutX, logoutY, logoutW, logoutH);
    
    // 背景和文字
    [logoutButton setBackgroundImage:[UIImage resizedImageWithName:@"common_button_red"] forState:UIControlStateNormal];
    [logoutButton setBackgroundImage:[UIImage resizedImageWithName:@"common_button_red_highlighted"] forState:UIControlStateHighlighted];
    [logoutButton setTitle:@"退出当前账号" forState:UIControlStateNormal];
    logoutButton.titleLabel.font = [UIFont systemFontOfSize:16.5f];
    [logoutButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [logoutButton addTarget:self action:@selector(logout) forControlEvents:UIControlEventTouchUpInside];
    
    // footer
    UIView *footer = [[UIView alloc] init];
    CGFloat footerH = logoutH + KYTableBorderW;
    footer.frame = CGRectMake(0, 0, 0, footerH);
    [footer addSubview:logoutButton];
    self.tableView.tableFooterView = footer;
}

- (void)setUpGroup0
{
    KYSettingGroup *group = [self addGroup];
    
    KYSettingArrowItem *userInfo = [KYSettingArrowItem itemWithTitle:NSLocalizedString(@"账户资料", nil) destVcClass:[KYUserInfoController class]];
    
    KYSettingArrowItem *mineStatus = [KYSettingArrowItem itemWithTitle:NSLocalizedString(@"我的动态", nil) destVcClass:[KYMineStatusController class]];
    
    group.items = @[userInfo,mineStatus];
}

- (void)logout {
    JSTAlertView *advertView = [[JSTAlertView alloc] initWithTitle:@"" message:@"是否退出当前账号" customView:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定"];
    [advertView setConfirmBlock:^{
         //0.清除个人信息
         [[KYUserAccountTool shareUserData] clearFromSandBox];
         //1.退出即时通讯登录状态
         [[SPKitExample sharedInstance] callThisBeforeISVAccountLogout];
         kAppDelegate.taobaoIMLogin = NO;
         //2.登出提醒
         [MBProgressHUD showSuccess:@"账号退出成功" toView:self.view];
     }];
    [advertView show];
}
@end
